<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="编号" prop="userNo">
      <el-input v-model="dataForm.userNo" placeholder="编号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="使用方式" prop="userUse">
      <el-input v-model="dataForm.userUse" placeholder="使用方式"></el-input>
    </el-form-item>
    <el-form-item label="是否可用" prop="userEnable">
      <el-input v-model="dataForm.userEnable" placeholder="是否可用"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="userMobile">
      <el-input v-model="dataForm.userMobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="userStatus">
      <el-input v-model="dataForm.userStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="入场时间" prop="userInTime">
      <el-input v-model="dataForm.userInTime" placeholder="入场时间"></el-input>
    </el-form-item>
    <el-form-item label="离场时间" prop="userOutTime">
      <el-input v-model="dataForm.userOutTime" placeholder="离场时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="userCreateTime">
      <el-input v-model="dataForm.userCreateTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="维护时间" prop="userUpdateTime">
      <el-input v-model="dataForm.userUpdateTime" placeholder="维护时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userNo: '',
          userName: '',
          userUse: '',
          userEnable: '',
          userMobile: '',
          userStatus: '',
          userInTime: '',
          userOutTime: '',
          userCreateTime: '',
          userUpdateTime: ''
        },
        dataRule: {
          userNo: [
            { required: true, message: '编号不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          userUse: [
            { required: true, message: '使用方式不能为空', trigger: 'blur' }
          ],
          userEnable: [
            { required: true, message: '是否可用不能为空', trigger: 'blur' }
          ],
          userMobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          userStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          userInTime: [
            { required: true, message: '入场时间不能为空', trigger: 'blur' }
          ],
          userOutTime: [
            { required: true, message: '离场时间不能为空', trigger: 'blur' }
          ],
          userCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          userUpdateTime: [
            { required: true, message: '维护时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/user/pgibmper/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userNo = data.pgibmper.userNo
                this.dataForm.userName = data.pgibmper.userName
                this.dataForm.userUse = data.pgibmper.userUse
                this.dataForm.userEnable = data.pgibmper.userEnable
                this.dataForm.userMobile = data.pgibmper.userMobile
                this.dataForm.userStatus = data.pgibmper.userStatus
                this.dataForm.userInTime = data.pgibmper.userInTime
                this.dataForm.userOutTime = data.pgibmper.userOutTime
                this.dataForm.userCreateTime = data.pgibmper.userCreateTime
                this.dataForm.userUpdateTime = data.pgibmper.userUpdateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/pgibmper/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userNo': this.dataForm.userNo,
                'userName': this.dataForm.userName,
                'userUse': this.dataForm.userUse,
                'userEnable': this.dataForm.userEnable,
                'userMobile': this.dataForm.userMobile,
                'userStatus': this.dataForm.userStatus,
                'userInTime': this.dataForm.userInTime,
                'userOutTime': this.dataForm.userOutTime,
                'userCreateTime': this.dataForm.userCreateTime,
                'userUpdateTime': this.dataForm.userUpdateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
